package com.fowo.api.sys.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fowo.api.entity.Warehouse;
import com.fowo.api.service.impl.WarehouseServiceImpl;
import com.fowo.api.sys.entity.SysUser;
import com.fowo.api.sys.entity.SysUserPermissionWarehouse;
import com.fowo.api.sys.mapper.SysUserPermissionWarehouseMapper;
import com.fowo.api.sys.model.SysDictAllItem;
import com.fowo.api.sys.model.SysUserPermissionWarehouseVo;
import com.fowo.api.sys.service.SysDictService;
import com.fowo.api.sys.service.SysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.fowo.api.sys.service.SysUserPermissionWarehouseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 仓库用户授权表服务接口实现
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-09-20 19:09:53
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysUserPermissionWarehouseServiceImpl extends ServiceImpl<SysUserPermissionWarehouseMapper, SysUserPermissionWarehouse> implements SysUserPermissionWarehouseService {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysDictService sysDictService;
    @Resource
    private WarehouseServiceImpl warehouseService;
    //仓库属性
    private final static String sysUserPermissionWarehouseDict = "SCM00017";


    /**
     * 获取全部仓库权限
     */
    @Override
    public List<SysUserPermissionWarehouse> getAllList(Long id) {
        List<SysUserPermissionWarehouse> result = new ArrayList<>();
        //01 获取对应字典
        List<SysDictAllItem> list = sysDictService.getDictItems(sysUserPermissionWarehouseDict);
        if (CollUtil.isEmpty(list)) {
            return result;
        }
        //02 循环字典赋值
        for (SysDictAllItem item : list) {
            List<Warehouse> warehouseList = warehouseService.list(Wrappers.<Warehouse>lambdaQuery().eq(Warehouse::getWidType, item.getItemValue()));
            if (CollUtil.isNotEmpty(warehouseList)) {
                List<SysUserPermissionWarehouse> sysUserPermissionWarehouse = warehouseList.stream().map(m -> new SysUserPermissionWarehouse(null, id, m.getWidType(), m.getId(), m.getName(), true)).collect(Collectors.toList());
                result.addAll(sysUserPermissionWarehouse);
            }
        }
        return result;
    }

    /**
     * 获取仓库权限
     *
     * @param id            用户id
     * @param selectWidType 选择的仓库类型
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<SysUserPermissionWarehouseVo> getList(Long id, String selectWidType, String keywords) {
        Assert.notNull(id, "用户参数异常!");
        List<SysUserPermissionWarehouseVo> result = new ArrayList<>();
        SysUser sysUser = sysUserService.getById(id);
        if (sysUser != null) {
            //01 获取对应字典
            List<SysDictAllItem> list = sysDictService.getDictItems(sysUserPermissionWarehouseDict);
            if (CollUtil.isEmpty(list)) {
                return result;
            } else if (StrUtil.isBlank(selectWidType)) {
                selectWidType = CollUtil.getFirst(list).getItemValue();
            }
            //02 循环字典赋值
            for (SysDictAllItem item : list) {
                SysUserPermissionWarehouseVo entity = new SysUserPermissionWarehouseVo(item.getItemValue(), item.getItemText(), 0, CollUtil.newArrayList());
                //02.1 当前选中
                if (StrUtil.isNotEmpty(selectWidType) && StrUtil.equalsIgnoreCase(selectWidType, item.getItemValue())) {
                    //02.2 选中的店铺+关键字
                    List<Warehouse> warehouseList = warehouseService.list(Wrappers.<Warehouse>lambdaQuery().eq(Warehouse::getWidType, item.getItemValue()).like(StrUtil.isNotBlank(keywords), Warehouse::getName, keywords));
                    if (CollUtil.isNotEmpty(warehouseList)) {
                        //02.2 在权限表存在:选中
                        List<Object> ids = this.listObjs(Wrappers.<SysUserPermissionWarehouse>lambdaQuery().eq(SysUserPermissionWarehouse::getUserId, id).eq(SysUserPermissionWarehouse::getWidType, selectWidType).select(SysUserPermissionWarehouse::getWarehouseId));
                        List<SysUserPermissionWarehouse> sysUserPermissionWarehouse = warehouseList.stream().map(m -> new SysUserPermissionWarehouse(null, id, m.getWidType(), m.getId(), m.getName(), ids.contains(m.getId()))).collect(Collectors.toList());
                        entity.setList(sysUserPermissionWarehouse);
                    }
                }
                //02.3 赋值数量(不受关键字影响)
                long count = warehouseService.count(Wrappers.<Warehouse>lambdaQuery().eq(Warehouse::getWidType, item.getItemValue()));
                entity.setCount(count);
                result.add(entity);
            }
        }
        return result;
    }

    /**
     * 保存仓库权限
     *
     * @param id   用户id
     * @param list 权限列表
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean create(Serializable id, List<SysUserPermissionWarehouse> list) {
        Assert.notNull(id, "用户参数异常!");
        SysUser sysUser = sysUserService.getById(id);
        if (sysUser != null) {
            this.remove(Wrappers.<SysUserPermissionWarehouse>lambdaQuery().eq(SysUserPermissionWarehouse::getUserId, id));
            return this.saveBatch(list);
        }
        return false;
    }
}